package com.jack.jetpackapp;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Jack 2021-05-08 00:06
 */
class DynamicProxy {
    private final Object object;

    public DynamicProxy(Object object) {
        this.object = object;
    }

    public <T> T create(final Class<T> clazz) {
        return (T) Proxy.newProxyInstance(
                clazz.getClassLoader(),
                new Class<?>[]{clazz},
                (o, method, objects) -> {
                    System.out.println("代理执行前");
                    Object result = method.invoke(object, objects);
                    System.out.println("代理执行后");
                    return result;
                }
        );
    }
}
